                header = next(reader)  # 读取标题行
                phone_index = header.index('手机号码')  # 找到'手机号码'列的索引
                location_index = header.index('配送地点')  # 找到'配送地点'列的索引
                exclude_columns = ['点单金额', '配送费', '优惠金额']  # 要排除的列名

                # 找到所有'选择配送时间点'列的索引
                time_columns = [i for i, col in enumerate(header) if '选择配送时间点' in col]

                # 找到所有包含'选择商家'且带有'【'和'】'符号的列的索引
                shop_columns = [i for i, col in enumerate(header) if '选择商家' in col and '【' in col and '】' in col]

                
                # 统计每一列非空单元格的数量, 并排除指定列，只统计到'end_index'列
                # 如果是新表单统计到"手机号码"列，旧表单统计到"配送地点"
                end_index = location_index if new_mode else phone_index
                non_empty_counts = [0] * (end_index + 1) 
                for row in reader:
                    for col_index, cell in enumerate(row[:end_index + 1]):
                        if header[col_index] in exclude_columns or col_index in time_columns or col_index in shop_columns:
                            continue
                        if cell.strip() != "":
                            non_empty_counts[col_index] += 1

                # 筛选非空列的标题和数据，排除指定列，只处理到'配送地点'列
                filtered_header = [header[i] for i in range(end_index + 1) if
                                non_empty_counts[i] > 0 and header[
                                    i] not in exclude_columns and i not in time_columns and i not in shop_columns]
                print("1",filtered_header)

                # 找到'选择下单类型'列的索引
                shop_type_index = filtered_header.index('选择下单类型')

                print("2",shop_type_index)
                

                # 将'选择配送时间点'和'选择商家'插入到'选择下单类型'列之后
                filtered_header.insert(shop_type_index + 1, '选择商家')
                filtered_header.insert(shop_type_index + 2, '选择配送时间点')

                print("3",filtered_header)
                print("4",filtered_header)

                # 添加'手机号码'列
                filtered_header.append('手机号码')

                writer.writerow(filtered_header)

                # 将文件指针重新定位到开头
                infile.seek(0)
                next(reader)
                
                data = []
                for row in reader:
                    # 筛选非空列的数据，排除指定列，只处理到'配送地点'列
                    filtered_row = [cell for i, cell in enumerate(row[:end_index + 1]) if
                                    non_empty_counts[i] > 0 and header[
                                        i] not in exclude_columns and i not in time_columns and i not in shop_columns]

                    # 获取所有'选择配送时间点'的值并去重
                    time_values = list(
                        set([row[i] for i in time_columns if row[i].strip() != ""]))
                    # 获取所有'选择商家'的值并去重
                    shop_values = list(
                        set([row[i] for i in shop_columns if row[i].strip() != ""]))

                    # 将'选择配送时间点'和'选择商家'插入到'选择下单类型'列之后
                    filtered_row.insert(shop_type_index + 1, ', '.join(shop_values))
                    filtered_row.insert(shop_type_index + 2, ', '.join(time_values))
                    
                    # 添加'手机号码'列的数据
                    filtered_row.append(row[phone_index])
                    
                    if filtered_row:
                        data.append(dict(zip(filtered_header, filtered_row)))